// --------------------------------------------------------------------------------------------------------------------
// <copyright file="frmScreenRegion.cs" company="">
//   
// </copyright>
// <summary>
//   The frm screen region.
// </summary>
// --------------------------------------------------------------------------------------------------------------------



using System;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Linq;
using System.Threading;
using System.Windows.Forms;

namespace vkUpploaderApp
{
    /// <summary>
    /// The frm screen region.
    /// </summary>
    public partial class frmScreenRegion : Form
    {
        #region Fields

        /// <summary>
        /// The click point.
        /// </summary>
        public Point ClickPoint = new Point();

        /// <summary>
        /// The current bottom right.
        /// </summary>
        public Point CurrentBottomRight = new Point();

        /// <summary>
        /// The current top left.
        /// </summary>
        public Point CurrentTopLeft = new Point();

        /// <summary>
        /// The left button down.
        /// </summary>
        public bool LeftButtonDown = false;

        /// <summary>
        /// The eraser pen.
        /// </summary>
        private readonly Pen EraserPen = new Pen(Color.FromArgb(255, 255, 192), 1);

        /// <summary>
        /// The my pen.
        /// </summary>
        private readonly Pen MyPen = new Pen(Color.Blue, 1);

        /// <summary>
        /// The g.
        /// </summary>
        private Graphics _graphics;

        /// <summary>
        /// The screen path.
        /// </summary>
        private string ScreenPath;

        #endregion

        #region Constructors and Destructors

        /// <summary>
        /// Initializes a new instance of the <see cref="frmScreenRegion"/> class.
        /// </summary>
        /// <param name="captureRegionSignal">
        /// The capture region signal.
        /// </param>
        public frmScreenRegion()
        {

            InitializeComponent();
            MouseDown += mouse_Click;
            MouseUp += mouse_Up;
            MouseMove += mouse_Move;
            MyPen.DashStyle = DashStyle.Dot;
        }

        #endregion

        #region Methods

        /// <summary>
        /// The button 1_ click.
        /// </summary>
        /// <param name="sender">
        /// The sender.
        /// </param>
        /// <param name="e">
        /// The e.
        /// </param>
        private void button1_Click(object sender, EventArgs e)
        {
            Close();
        }

        /// <summary>
        /// The frm screen region_ key up.
        /// </summary>
        /// <param name="sender">
        /// The sender.
        /// </param>
        /// <param name="e">
        /// The e.
        /// </param>
        private void frmScreenRegion_KeyUp(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Escape) {
                DialogResult = DialogResult.Abort;
            }
        }

        /// <summary>
        /// The mouse_ click.
        /// </summary>
        /// <param name="sender">
        /// The sender.
        /// </param>
        /// <param name="e">
        /// The e.
        /// </param>
        private void mouse_Click(object sender, MouseEventArgs e)
        {
            _graphics.Clear(Color.FromArgb(255, 255, 192));
            LeftButtonDown = true;
            ClickPoint = new Point(e.X, e.Y);
        }

        /// <summary>
        /// The mouse_ move.
        /// </summary>
        /// <param name="sender">
        /// The sender.
        /// </param>
        /// <param name="e">
        /// The e.
        /// </param>
        private void mouse_Move(object sender, MouseEventArgs e)
        {
            // Resize (actually delete then re-draw) the rectangle if the left mouse button is held down
            if (LeftButtonDown) {
                // Erase the previous rectangle
                _graphics.DrawRectangle(EraserPen, CurrentTopLeft.X, CurrentTopLeft.Y, CurrentBottomRight.X - CurrentTopLeft.X, CurrentBottomRight.Y - CurrentTopLeft.Y);

                // Calculate X Coordinates
                if (e.X < ClickPoint.X) {
                    CurrentTopLeft.X = e.X;
                    CurrentBottomRight.X = ClickPoint.X;
                }
                else {
                    CurrentTopLeft.X = ClickPoint.X;
                    CurrentBottomRight.X = e.X;
                }

                // Calculate Y Coordinates
                if (e.Y < ClickPoint.Y) {
                    CurrentTopLeft.Y = e.Y;
                    CurrentBottomRight.Y = ClickPoint.Y;
                }
                else {
                    CurrentTopLeft.Y = ClickPoint.Y;
                    CurrentBottomRight.Y = e.Y;
                }

                // Draw a new rectangle
                _graphics.DrawRectangle(MyPen, CurrentTopLeft.X, CurrentTopLeft.Y, CurrentBottomRight.X - CurrentTopLeft.X, CurrentBottomRight.Y - CurrentTopLeft.Y);
            }
        }

        /// <summary>
        /// The mouse_ up.
        /// </summary>
        /// <param name="sender">
        /// The sender.
        /// </param>
        /// <param name="e">
        /// The e.
        /// </param>
        private void mouse_Up(object sender, MouseEventArgs e)
        {
            LeftButtonDown = false;
            Hide();
            Thread.Sleep(255);
            Application.DoEvents();

            var startPoint = new Point(CurrentTopLeft.X, CurrentTopLeft.Y);
            startPoint = PointToScreen(startPoint);
            var bottomRight = PointToScreen(CurrentBottomRight);
            var bounds = new Rectangle(startPoint.X, startPoint.Y, bottomRight.X - startPoint.X, bottomRight.Y - startPoint.Y);
            frmTray frm = Application.OpenForms.OfType<frmTray>().First();

            frm.CaptureImage(startPoint, bounds);
            DialogResult = DialogResult.Yes;
        }

        #endregion

        private void frmScreenRegion_Shown(object sender, EventArgs e)
        {
            _graphics = CreateGraphics();
        }

        private void frmScreenRegion_Resize(object sender, EventArgs e)
        {
            _graphics = CreateGraphics();
        }
    }
}